using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
//TODO add timers
//TODO add saveable encryption
//TODO system tray

namespace UA_Dorm_Ethernet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            GetCurrentNetworkInfo();

            if (txtUsername.Text.Replace(" ", "").Length < 2)
            {
                MessageBox.Show("Your UAnet ID appears to be incorrect.");
                return;
            }
            if (txtPassword.Text.Length < 6)
            {
                MessageBox.Show("Your Password appears to be incorrect.");
                return;
            }

                

            txtOutput.Text = "https://" + txtUsername.Text + ":" + txtPassword.Text + "@gozips.uakron.edu/zid/user/connect.cgi";

            if (PingTest())
                if (Login(txtUsername.Text, txtPassword.Text))
                {
                    //set timerinto motion;
                }
        }

        private bool Login(string Username, string Password)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "wget.exe";
            proc.StartInfo.Arguments = "https://" + Username + ":" + Password + "@gozips.uakron.edu/zid/user/connect.cgi -o lastlog.log --no-check-certificate --spider";
            proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            try 
            { 
                proc.Start();
                proc.WaitForExit(); //this keeps it from reading the previous log of wget.
                StreamReader srOutput = File.OpenText("lastlog.log");
                string read = null;
                txtOutput.Text = "";
                while ((read = srOutput.ReadLine()) != null)
                {
                    txtOutput.Text += read;
                    if (read.Contains("Authorization failed."))
                    {
                        MessageBox.Show("UAnet ID and/or Password are incorrect");
                        return false;
                    }
                }
                srOutput.Close();
                return true;
            }
            catch { txtOutput.Text = "Troubles running file \"wget.exe\".";  }
            return false;
        } //logins in to gozips

        private bool PingTest() //pings a website for connectivity
        {
            /* Easy Ping
             * Date Created: 10/3/2006 7:22:41 PM
             * Original Author: Kelly S. Elias
             * URL: http://devdistrict.com/codedetails.aspx?A=396
             */
            System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
            try
            {
                System.Net.NetworkInformation.PingReply reply = ping.Send("www.uakron.edu", 10000);
                //MessageBox.Show("Address:" + reply.Address + "\nTime: " + reply.RoundtripTime);
                return true;
            }
            catch { return false; }
        }

        private void GetCurrentNetworkInfo()
        {
            string strhostname = System.Net.Dns.GetHostName();
            //System.Net.Dns.GetHostAddresses(strhostname);
            bool HasUAnetIP = false;
            for(int x = 0; x > System.Net.Dns.GetHostAddresses(strhostname).Length; x++)
            {
                if (System.Net.Dns.GetHostAddresses(strhostname)[x].ToString().Contains("130.101."))
                    HasUAnetIP = true;
            }

            if (!HasUAnetIP)
            {
                //change to ok/cancel or yes/no box to allow override
                MessageBox.Show("Warning!! You do not appear to have UA network assigned IP address!");
            }


        }

    }
}